package com.zs.ktsms.controller;

import com.zs.ktsms.pojo.Address;
import com.zs.ktsms.service.AddressService;
import com.zs.ktsms.utils.Page;
import com.zs.ktsms.utils.ResponseMessage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/31 下午9:30
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService service;

    @GetMapping("/list")
    public ResponseMessage<Page<Address>> select(String contact, String userName, @RequestParam(name = "pageNo" ,defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        return ResponseMessage.success("地址列表加载成功",service.getPage(contact,userName,pageNo,pageSize));
    }


    @PostMapping("/add")
    public ResponseMessage add(@RequestBody Address address){
        int i = service.add(address);
        if(i>0){
            return ResponseMessage.success("添加成功");
        }else{
            return ResponseMessage.error("添加失败");
        }
    }

    @GetMapping("/detail")
    public ResponseMessage<Address> detail(Integer id){
        return ResponseMessage.success("地址详情加载成功",service.detail(id));
    }

    @PostMapping("/update")
    public ResponseMessage update(@RequestBody Address address){
        int i = service.update(address);
        if(i>0){
            return ResponseMessage.success("修改成功");
        }else{
            return ResponseMessage.error("修改失败");
        }
    }

    @DeleteMapping("/del")
    public ResponseMessage delete(Integer id){
        int i = service.delete(id);
        if(i>0){
            return ResponseMessage.success("删除成功");
        }else{
            return ResponseMessage.error("删除失败");
        }
    }

}
